/*
 * main.c
 *
 *  Created on: Apr 6, 2015
 *      Author: mxw
 */
#include <KeyDrv.h>
#include "LedDrv.h"
#include "UartDrv.h"
#include <eventQueue.h>
#include <timer.h>
#include <schedule.h>

QueMgr g_Main_Queue;

#define YINJIE_1 2981
#define YINJIE_2 2657
#define YINJIE_3 2367
#define YINJIE_4 2232
#define YINJIE_5 1992
#define YINJIE_6 1775
#define YINJIE_7 1581

extern void Buzzer_Freq_Set(unsigned int freq );
extern void new_muisc_go();
extern void request_to_stop();
extern void init_music();
int main();

void exec_cmd(Queue_Element* pe) {
    switch (pe->cmd) {
    case CMD_UART_RECEVEI_DONE:
        if ('\n' == pe->data[0] || '\r' == pe->data[0]) {
            uart_print("\r\n");
        } else {
            uart_print_char(pe->data[0]);
        }

        if ('1' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            uart_print("\r\nLED1 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_1);
        } else if ('2' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_2, LED_ON);
            uart_print("\r\nLED2 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_2);
        } else if ('3' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED3 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_3);
        } else if ('4' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_4, LED_ON);
            uart_print("\r\nLED4 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_4);
        } else if ('5' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED13 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_5);
        } else if ('6' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_2, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED23 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_6);
        } else if ('7' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            set_led(LED_2, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED123 is on!\r\n");
            Buzzer_Freq_Set(YINJIE_7);
        } else if ('m' == pe->data[0]) {
            new_muisc_go();
        } else if ('s' == pe->data[0]) {
            request_to_stop();
        }
        break;
    case CMD_KEY1_DOWN:
        key1_down();
        break;
    case CMD_KEY2_DOWN:
        key2_down();
        break;
    case CMD_KEY3_DOWN:
        key3_down();
        break;
    case CMD_KEY4_DOWN:
        key4_down();
        break;
    case CMD_TIMER_TIMEOUT:
        call_timer_func(pe->data[0]);
        break;
    default:
        break;
    }
}

void on_main_timer_test(unsigned short tid) {
    uart_print("__good!____\r\n");
}


void pre_main() {
    init_task();
    main();
}

int main() {
    Queue_Element* pe = 0;
    unsigned short tid = 0;

    init_timer();
    init_key();
    init_led();
    uart_init(115200);
    init_event_queue(&g_Main_Queue);
    set_led(LED_1, LED_ON);
    init_music();

    init_INT_TIMER1_for_simos();

    uart_print("hello world!\r\n");
    set_led(LED_2, LED_ON);
    uart_print("你好!好\r\n");
    uart_print("こんにちは、しばらくお待ちください。\r\n");

    tid = get_new_timer(on_main_timer_test);
    if (ERR_TIMER_ID == tid) {
        uart_print("get test_Timer Failed!\r\n");
    } else {
        uart_print("get test_Timer success!\r\n");
    }
    start_timer(tid, 5000); //5s timer

    while(1) {
//        while(i < 0xFFFF) {
//            i++;
//        }
//        i = 0;
//        uart_print("B");
//        set_led(LED_ALL, LED_OFF);
//        set_led(LED_3, LED_ON);

        pe = pick_msg(&g_Main_Queue);
        while(pe) {
            exec_cmd(pe);
            pe = pick_msg(&g_Main_Queue);
        }
    }
}

































